{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73a2d003",
   "metadata": {},
   "source": [
    "# 人脸识别实践——宿舍智能识别门禁系统（xx栋）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a5bc053",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    "\n",
    "1.创建FaceSet\n",
    "2.查询FaceSet\n",
    "3.存入face_token数据\n",
    "4.获取FaceSet信息数据\n",
    "5.人脸搜索\n",
    "6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c869f656",
   "metadata": {},
   "source": [
    "## 创建Faceset\n",
    "* 描述\n",
    "> 1. 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    "> 2. 试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。\n",
    "* 调用url\n",
    "> 1. https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "* 调用方法\n",
    "> 1. POST\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b8a9294e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d4afd3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&amp;=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&amp;=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "116c9fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f8e52f75",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80cfb0d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f2cd2d45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "20046ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a1102f5",
   "metadata": {},
   "source": [
    "## 查询数据库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc3dbef",
   "metadata": {},
   "source": [
    "* 描述\n",
    "> 1.  获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。\n",
    "> 2.  注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。\n",
    "* 调用URL\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "94f17057",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>包含需要查询的FaceSet标签的字符串，用逗号分隔</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选         参数名      类型                                               参数说明\n",
       "0   必选     api_key  String                                     调用此API的API Key\n",
       "1   必选  api_secret  String                                  调用此API的API Secret\n",
       "2   可选        tags  String                         包含需要查询的FaceSet标签的字符串，用逗号分隔\n",
       "3   可选       start     Int  一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号..."
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">包含需要查询的FaceSet标签的字符串，用逗号分隔</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号。</span></p><p><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 faceset_token 开始返回。返回的 faceset_token 按照创建时间排序。每次返回<span class=\"s1\">1000</span>个<span class=\"s1\">FaceSets</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为1。</span></p><p class=\"p1\"><span style=\"color: rgb(255,0,0);\">注意：2017年8月16日后，每次返回的 FaceSet 数量上限从 1000 改为 100。并且您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 faceset_token。请尽快修改调整您的程序。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "499fc8d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a335f76c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c61b69e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e591ffd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "28bc411c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fa05d912",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "528f5872",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b54a1a0e78c82d96e452853094e60553'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['facesets'][-1]['faceset_token']#取值\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75338640",
   "metadata": {},
   "source": [
    "## 更新人脸库信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6c59ef",
   "metadata": {},
   "source": [
    "* 描述  \n",
    "> 更新一个人脸集合的属性  \n",
    "\n",
    "* 调用URL  \n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/update  \n",
    "\n",
    "* 调用方法  \n",
    "> POST  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "129d461e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "258d777a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "Face_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':'faceset_token',\n",
    "    'display_name':'广州南方学院37栋学生宿舍楼',\n",
    "    'new_outer_id':'NFUSD37',\n",
    "    'user_data':'6层，满员，女生宿舍，460位同学，已录face_token'\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = Face_update_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dcaebb3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "99210b6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 64,\n",
       " 'error_message': 'INVALID_FACESET_TOKEN',\n",
       " 'request_id': '1649067102,08675ab6-4dd5-49c6-9978-073cb6f16944'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "61e32788",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2c9c795f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "Face_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':'faceset_token',\n",
    "    'new_outer_id':'NFUSD37',\n",
    "    'user_data':'6层，满员，女生宿舍，460位同学，已录face_token'\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = Face_getdetail_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "59809cf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 62,\n",
       " 'error_message': 'INVALID_FACESET_TOKEN',\n",
       " 'request_id': '1649067117,09d72d32-895f-4637-8c32-f1a7bbea1a3b'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55b9b380",
   "metadata": {},
   "source": [
    "## 人脸检测，获取人脸token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "40bf5814",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "dc5c8bc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    d:\\2022-summer-api\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ad027f02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649067125,ff7a2893-1615-4bcd-9682-7b3533ced773',\n",
       " 'time_used': 88,\n",
       " 'faces': [{'face_token': 'ecd70608fe9760f29c51021c0186ce4e',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9bc7908",
   "metadata": {},
   "source": [
    "## 人脸存放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9be9fbe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "FaceSet_addface_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "\n",
    "# 3. POST（请求方法）\n",
    "\n",
    "# 4.准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'outer':'NFUSD37',\n",
    "    'face_token':'b1d25b6aad0939a0367d61034ce5ab38',\n",
    "\n",
    "}\n",
    "\n",
    "# 5.发起请求\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bc5846f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "31b9d27e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649068757,c3a636d8-d08b-4047-81da-2869a308624d',\n",
       " 'time_used': 93,\n",
       " 'faces': [{'face_token': '77f3bf982c219c8ce43ca452611ca86b',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key ,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e60dc896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0efdf1fe783dc0ff610baad63ff95b96'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_Liu = detect.face_detect(API_key,API_sercret,'Liu_01.jpeg')['faces'][0]['face_token']\n",
    "face_token_Liu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5cec2e6",
   "metadata": {},
   "source": [
    "# compare.py功能的调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df047f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d8e770ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649069214,45929ad5-b0d9-4b6a-81e2-46b0c92b81a4',\n",
       " 'time_used': 54,\n",
       " 'error_message': 'MISSING_ARGUMENTS: face_token1, image_base64_1, image_url1, image_file1'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "API_key = 'gDcTneRPoWnCE3xqz3TxW30bkoFuVQDR'\n",
    "API_sercret = 'ATxKDVKmriBgj1nrbXMv5fgEoAGGGFLz'\n",
    "compare.face_compare(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f905c345",
   "metadata": {},
   "source": [
    "# search.py功能的调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "33b0c8b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "fc83e270",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 57,\n",
       " 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id',\n",
       " 'request_id': '1649069233,c042c9c9-2825-4888-8e82-31b999f357ab'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "424ada00",
   "metadata": {},
   "source": [
    "# analyze.py的调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d5a08dc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3c3d780a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 65,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1649069279,7e824e4b-5b6a-4647-86f1-39a32c7f11b3'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fe10683",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3ee6896",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
